#include <MainWindow.h>
#include <ui_MainWindow.h>

#include <QFileDialog>

#include <iostream>

MainWindow::MainWindow(QWidget* parent): QMainWindow(parent), ui(new Ui::MainWindow)
{
  ui->setupUi(this);

  SetActions();
  
  db = NULL;
  ui->dateEditDate->setDate(QDate::currentDate());
  ui->textEditTextView->setReadOnly(true);
}

MainWindow::~MainWindow(void)
{
  delete ui;
  if(db != NULL)
	 delete db;
}

void MainWindow::SetActions(void)
{
  // MenuBar actions.
  connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(ActionExit()));
  connect(ui->actionCreate, SIGNAL(triggered()), this, SLOT(ActionCreate()));
  connect(ui->actionConnect, SIGNAL(triggered()), this, SLOT(ActionConnect()));
  connect(ui->actionDisconnect, SIGNAL(triggered()), this, SLOT(ActionDisconnect()));
}

void MainWindow::ConnectDataBase(QString dbname, bool create)
{
  if(dbname != NULL)
  {
	 // If database does not exist, create new one.
	 db = new TextDB(dbname.toAscii().data(), create);

	 // Set the label with the database name.
	 SetLabelDBName(dbname, "color: rgb(0, 0, 0)", db->GetCreationDate());
  }
}

void MainWindow::SetLabelDBName(QString dbname, QString color, QString creation_date)
{
  ui->labelDBName->setStyleSheet("QLabel {"+color+";}");
  if(dbname != "")
  {
	 QStringList path = dbname.split("/");
	 dbname = path[path.size()-1];
	 ui->labelDBName->setText("BAZA DANYCH: "+dbname+"; DATA UTWORZENIA: "+creation_date);
  }
  else
	 ui->labelDBName->setText("BAZA DANYCH: "+dbname);
}

void MainWindow::EnableComponents(bool enabled)
{
  QString color = (enabled) ? "color: rgb(0, 0, 0)" : "color: rgb(171, 171, 171)";
  
  ui->labelDate->setStyleSheet("QLabel {"+color+";}");
  ui->labelCategory->setStyleSheet("QLabel {"+color+";}");
  ui->labelProducts->setStyleSheet("QLabel {"+color+";}");
  ui->dateEditDate->setDate(QDate::currentDate());
  ui->dateEditDate->setEnabled(enabled);
  ui->lineEditCategory->setEnabled(enabled);
  ui->textEditProducts->setEnabled(enabled);
  ui->pushButtonAdd->setEnabled(enabled);
  ui->pushButtonClear->setEnabled(enabled);
  ui->labelTextView->setStyleSheet("QLabel {"+color+";}");
  ui->textEditTextView->setEnabled(enabled);
  
  QString textdb = (enabled) ? db->GetDataBase(true) : "";
  ui->textEditTextView->setText(textdb);
  comment = ui->textEditTextView->toPlainText();
}

void MainWindow::ActionCreate(void)
{
  // Create database with given name.
  QString dbname = QFileDialog::getSaveFileName(this, tr("Create Database"), QDir::currentPath());
  ConnectDataBase(dbname, true);
  EnableComponents(true);
}

void MainWindow::ActionConnect(void)
{
  // Connect to database with given name.
  QString dbname = QFileDialog::getOpenFileName(this, tr("Open Database"), QDir::currentPath());
  ConnectDataBase(dbname, false);
  EnableComponents(true);
}

void MainWindow::ActionDisconnect(void)
{
  db->Disconnect();
  
  // Clear the label with the database name.
  SetLabelDBName("", "color: rgb(171, 171, 171)", "");
  EnableComponents(false);
}

void MainWindow::ActionExit(void)
{
  this->close();
}

void MainWindow::ButtonAddPressed(void)
{
  db->AddProduct(ui->dateEditDate->text().trimmed().toAscii().data(), 
					  ui->lineEditCategory->text().trimmed().toAscii().data(), 
					  ui->textEditProducts->toPlainText().trimmed().toAscii().data());

  //QString comment = ui->textEditTextView->toPlainText()+"\n";
  
  ui->textEditTextView->clear();
  ui->textEditTextView->setText(comment+db->GetDataBase(false));
}

void MainWindow::ButtonClearPressed(void)
{
  ui->lineEditCategory->setText("CLEAR");
}